home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 118_01.zip / CONVR2C.DOC < prev    next >
Text File  |  1993-06-03  |  6KB  |  235 lines

  1. .cm How to convert from RATFOR to C
  2. .cm
  3. .cm source:  convr2c.doc
  4. .cm version: May 22, October 14, 1981.
  5. .bp 1
  6. .pl 66
  7. .rm 60
  8. .he 'convr2c'RATFOR to C conversions'%'
  9. .fo ''-#-''
  10.  
  11. .ul
  12. introduction
  13.  
  14. .ti +3
  15. This file tells what I had to do to convert a large
  16. program (ROFF) from RATFOR to C.
  17. Most of this file was written just after I finished the
  18. project, so it is quite complete.
  19.  
  20. .ul
  21. general changes
  22.  
  23. .ti +3
  24. Comments must be changed from RATFOR style to C style.
  25. RAT4 comments start with # (outside strings) and
  26. continue to the end of the line.
  27. C comments start with /* and continue to */.
  28. The program rat2c converts from RAT4 comments to C comments.
  29.  
  30. .ti +3
  31. Multiple spaces may be replaced with tabs.
  32. The routines entab() and detab() are only marginally useful
  33. for this.
  34.  
  35. .ti +3
  36. RATFOR define statment must be replaced by the C #define.
  37. The RATFOR define looks like:
  38.  
  39.     define (a,b)
  40.  
  41. The C define looks like:
  42.  
  43. #define a b
  44.  
  45. Note that the C define must start in column 1.
  46. No arithmetic is allowed in the C define.
  47.  
  48. .ti +3
  49. RAT4 had the ifdef and ifnotdef pseudo ops.
  50. These may have to be changed in some versions of C.
  51. They can be replaced by just commenting out code
  52. or by creating runtime global variables corresponding
  53. to compile time variables tested by the ifdef.
  54.  
  55. .ti +3
  56. RAT4 uses includes inside each program for including
  57. declarations of variables.
  58. In C, these  #includes appear once at the start of each file.
  59. The includes inside each RAT4 program must be commented out
  60. and replaced by a single set at the start of the C file.
  61.  
  62. .ti +3
  63. Printable character constants can be eliminated from
  64. C programs.
  65. I think that RAT4 uses them to get around problems with
  66. character sets.
  67. At the present time I can see no reason to have constants
  68. for printable characters.
  69. I may be wrong though.
  70.  
  71. .ti +3
  72. RAT4 common blocks must be replaced by global variables
  73. declared in a header file.
  74. Array dimensions must appear in the variable declarations
  75. in C rather than in the common block declaration.
  76.  
  77. .ul
  78. changes to declarations
  79.  
  80. .ti +3
  81. The RAT4 keywords function and subroutine
  82. must be eliminated where they appear.
  83.  
  84. .ti +3
  85. RAT4 groups parameter declarations with declarations of
  86. local variables.
  87. In C, these two declarations are separated by a {.
  88.  
  89. .ti +3
  90. The RAT4 keyword integer and character must be changed to
  91. int and char.
  92.  
  93. .ti +3
  94. The RAT4 keyword pointer indicates (probably) that the
  95. variable is an int whose address will be passed (via &)
  96. as an actual parameter.
  97. It is not clear whether the RAT4 usage of pointer is
  98. consistent with good C coding practice.
  99.  
  100. .ti +3
  101. As mentioned before, include statements inside RAT4
  102. subroutines must be eliminated.
  103.  
  104. .ti +3
  105. The RAT4 data statement can not be simulated directly
  106. without C initializers.
  107. An alternative is to create global variables which are
  108. initialized at run time.
  109.  
  110. .ti +3
  111. The names of functions which are used by a RAT4 subroutine
  112. appear in the declarations of that routine.
  113. These declarations must be eliminated from C programs.
  114. (The C compiler thinks local variables are being declared.)
  115.  
  116. .ti +3
  117. RAT4 uses parens to declare arrays.
  118. C uses square brackets.
  119.  
  120. .ul
  121. changes to executable statements
  122.  
  123. .ti +3
  124. The biggest problem is probably subscripts.
  125. (See below for a more semantic problem with subscripts).
  126. RAT4 uses parens for both actual parameter lists and
  127. array subscripts.
  128. C uses parens for parameter lists, but square brackets
  129. for subscripts.
  130. Clearly, whether to convert to square brackets depends on
  131. knowing whether an id is an function or not.
  132.  
  133. .ti +3
  134. In C, all statements must end in semicolons.
  135. Once again, a full RAT4 parser is needed in order to
  136. know where to put the semicolons.
  137.  
  138. .ti +3
  139. RAT4 assignment statements like:
  140.  
  141. .in +5
  142. i = i + 1
  143. .br
  144. .in -5
  145.  
  146. can be changed to:
  147.  
  148. .in +5
  149. i++;
  150. .br
  151. .in -5
  152.  
  153. .ti +3
  154. In RAT4 functions, the name of the function is used
  155. as a variable which denotes the value returned by the function.
  156. In C, the name of a function denotes recursive execution.
  157. In C, a new local variable must be declared inside the
  158. function and the function must return a value with a 
  159. return(value) statement.
  160.  
  161. .ti +3
  162. My version of C does not have a repeat statement.
  163. The RAT4 statement:
  164.  
  165. .in +5
  166. repeat { ...  } until ( )
  167. .br
  168. .in -5
  169.  
  170. must be replaced by the C statement:
  171.  
  172. .in +5
  173. while (1) { ...  if ( ) break; }
  174. .br
  175. .in -5
  176.  
  177. .ti +3
  178. The RAT4 statement next must be replaced by
  179. the C statement continue.
  180.  
  181. .ti +3
  182. RAT4 uses call statement. C does not.
  183. RAT4 calls with no arguments do not need actual parameter
  184. lists.
  185. In C, a function with no arguments still needs ().
  186.  
  187. .ti +3
  188. In RAT4 all arrays start at 1.
  189. In C all arrays start at 0.
  190. This means that array declarations will have to be changed,
  191. as well as all code that refers to the limits of arrays.
  192. In practice, a semantic analysis of how
  193. .ul
  194. all
  195. variables of a program are used is required.
  196. Almost every part of a program might need to be changed:
  197. initialization of variables, for loop indices, actual
  198. parameters, etc.
  199.  
  200. .ti +3
  201. RAT4 uses call by reference.
  202. C uses call by value.
  203. This means that if a RAT4 subroutine changes any of it's
  204. arguments, the C routine will have to pass a pointer
  205. to a variable, rather than the variable itself.
  206. The C routine will have to change the variable via
  207. indirection.
  208. This also means that C programs do not have to protect
  209. there arguments from change like RAT4 programs do.
  210.  
  211. .ti +3
  212. RAT4 uses & and | to indicate AND and OR.
  213. C uses && and || to indicate boolean AND and OR.
  214. C can use & and | only if each relational clause
  215. joined by & and | is fully parenthesized.
  216.  
  217. .ti +3
  218. For best portability RAT4 uses the construction:
  219.  
  220. .in +5
  221. junk = func()
  222. .br
  223. .in -5
  224.  
  225. even when junk is not going to be used.
  226. This is not needed in C.
  227.  variable via
  228. indirection.
  229. This also means that C programs do not have to protect
  230. there arguments from change like RAT4 programs do.
  231.  
  232. .ti +3
  233. RAT4 uses & and | to indicate AND and OR.
  234. C uses && and || to indicate boolean AND and OR.
  235. C can use & an